package com.hzdl.hotel.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.hotel.bean.Check;
import com.hzdl.hotel.service.CheckService;
import com.hzdl.hotel.util.AJAXResult;
import com.hzdl.hotel.util.RentExcelUtil;
import com.hzdl.hotel.vo.CheckVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Controller
@RequestMapping("/check")
public class CheckController {

    @Autowired
    private CheckService cs;

    @RequestMapping("/checkInit")
    public String checkInit(){
        return "administer/check";
    }

    @RequestMapping("/checkShow")
    @ResponseBody
    public AJAXResult checkShow(CheckVo checkVo){
        if (checkVo.getPage() == null) {
            checkVo.setPage(1);
        }
        if (checkVo.getLimit() == null) {
            checkVo.setLimit(5);
        }
        PageInfo pageInfo = cs.findCheckByWhere(checkVo);
        return new AJAXResult(0, "", pageInfo.getList(), pageInfo.getTotal());
    }

    @RequestMapping("/exportInfo")
    public ResponseEntity<Object> exportInfo(String checkId){
        Check check=cs.findByCheckId(checkId);
        ByteArrayOutputStream stream = RentExcelUtil.createExcelByRent(check,"czd-templet.xls");
        String fileName = null;
        //导出文件名称
        try {
            fileName = URLEncoder.encode("客户信息.xls", "UTF-8");
        } catch (
                UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //设置下载是的头部响应对象
        HttpHeaders httpHeader = new HttpHeaders();
        httpHeader.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        httpHeader.setContentDispositionFormData("attachment", fileName);

        return new ResponseEntity<Object>(stream.toByteArray(), httpHeader, HttpStatus.CREATED);
    }


}
